<?php

namespace pmvc\log;

class LoggerManager {

	private static $INSTANCE = null;

	private $loggers;
	private $defaultLevel;
	private $defaultAppender;

	private function __construct() {
		$this->loggers = Array();
	}

	public static function getInstance() {
		if (!isset(LoggerManager::$INSTANCE)) {
			$mgr = new LoggerManager();
			$mgr->setDefaultAppender(new DefaultAppender());
			$mgr->setDefaultLevel(Logger::WARN);
			LoggerManager::$INSTANCE = $mgr;
		}
		return LoggerManager::$INSTANCE;
	}

	public static function configure($file, $level=Logger::WARN, $format=DefaultFormatter::BASIC_LOG_FORMAT) {
		$mgr = !isset(LoggerManager::$INSTANCE) ? new LoggerManager() : LoggerManager::$INSTANCE;
		$mgr->setDefaultAppender(DefaultAppender::newInstance($file, $format));
		$mgr->setDefaultLevel($level);
		LoggerManager::$INSTANCE = $mgr;
	}
	
	public function getLogger($name) {
		if (!isset($this->loggers[$name])) {
			$this->loggers[$name] = new Logger(
				$name, $this->defaultLevel, $this->defaultAppender);
		}
		return $this->loggers[$name];
	}

	public function setDefaultAppender(Appender $appender) {
		$this->defaultAppender = $appender;
	}

	public function setDefaultLevel($level) {
		$this->defaultLevel = $level;
	}
}

?>